<template lang="html">
    <div class="container">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                    <el-input class="form_input" v-model="searchText" placeholder="请输入姓名或ID"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" :disabled="loading" @click="onSubmit">搜索</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables" v-loading="loading">
          <el-table :data="tableData" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column prop="id" label="ID" align="center"></el-table-column>
                <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
                <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
                <el-table-column prop="birthday" label="出生年月" align="center"></el-table-column>
                <el-table-column prop="levelName" label="会员等级" align="center"></el-table-column>
                <el-table-column prop="integralValue" label="积分" align="center"></el-table-column>
                <el-table-column prop="prepaidBalance" label="余额" align="center"></el-table-column>
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>

        <el-dialog title="用户编辑" v-loading="dialogLoading" append-to-body :visible.sync="dialogVisible" width="30%" >
            <div class="form">
                <div class="form_item">
                    <span>姓名</span>
                    <div class="value">{{currentData.userName}}</div>
                </div>
                <div class="form_item">
                    <span>手机号</span>
                    <div class="value">{{currentData.phone}}</div>
                </div>
                <div class="form_item">
                    <span>ID</span>
                    <div class="value">{{currentData.id}}</div>
                </div>
                <div class="form_item">
                    <span>会员等级</span>
                    <el-select v-model="newForm.levelId" placeholder="请选择">
                      <el-option v-for="item in vipOptions" :key="item.id" :label="item.levelName" :value="item.id"></el-option>
                    </el-select>
                </div>
                <div class="form_item">
                    <span>积分</span>
                    <el-input v-model="newForm.integralValue"></el-input>
                </div>
                <div class="form_item">
                    <span>预付结余</span>
                    <el-input v-model="newForm.prepaidBalance"></el-input>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleConfirm">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
export default {
  data(){
    return {
      loading:false,
      dialogLoading:false,
      searchText: '',
      currentPage: 1,
      totalPage: 0,
      pageSize:10,
      tableData: [],
      currentData: {},
      vipOptions: [],
      dialogVisible: false,
      newForm: {levelId:null,integralValue:null,prepaidBalance:null},
    }
  },
  created(){
    this.selectMemberPage();
  },
  methods:{
    async selectMemberPage(){
      this.loading = true;
      let res = await this.$http.selectUserMemberPage({
        query: this.searchText,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      });
      this.loading = false;
      this.tableData = res.data.rows;
      this.totalPage = res.data.total;
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectMemberPage();
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    onSwitch(e){
      this.$http.closeMember({id:e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      });
    },
    handleConfirm(){
      this.dialogLoading = true;
      this.$http.updateMember({
        id: this.currentData.id,
        levelId: this.newForm.levelId,
        integralValue: this.newForm.integralValue,
        prepaidBalance: this.newForm.prepaidBalance,
      }).then(res=>{
        this.dialogLoading = false;
        this.dialogVisible = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.selectMemberPage();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    fEdit(e){
      this.currentData = Object.assign({},e);
      this.$http.selectMemberLevels().then(res=>{
        if(res.data.code == 0){
          this.dialogVisible = true;
          this.vipOptions = res.data.data;
          this.newForm.levelId = this.currentData.levelId;
          this.newForm.integralValue = this.currentData.integralValue;
          this.newForm.prepaidBalance = this.currentData.prepaidBalance;
        }
      });
    },

  }

}
</script>

<style lang="css" scoped>
.form{
  padding: 0 50px;
}
.search{
  margin-bottom: 10px;
}
.form  .form_item{
  display: flex;
  line-height: 40px;
}
.form_item .value{
  width: 100%;
}
/deep/.form_item .el-select{
  width: 100%;
}
.form .radio_box{
  width: 100%;
  padding-left: 30px;
  box-sizing: border-box;
}
.form  .form_item>span:nth-child(1){
  display: inline-block;
  width: 150px;
  text-align: right;
  padding-right: 20px;
}
</style>
